package com.scientific.research.gobang.service.robot.impl;

import com.scientific.research.gobang.Utils.RedisUtil;
import com.scientific.research.gobang.pojo.User;
import com.scientific.research.gobang.service.CodeAndCodeService;
import com.scientific.research.gobang.service.RobotService;
import com.scientific.research.gobang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class RobotServiceImpl implements RobotService {

    @Autowired
    UserService userService;
    @Resource(name = "codeAndCodeServiceImpl")
    CodeAndCodeService codeAndCodeService;
    @Autowired
    RedisUtil redisUtil;

    //每一次落子都会调用codeAndCodeService.ManAndCode方法，负责判断有没有成局，如果赢了或者输了就返回结果；如果没成局就将棋盘信息写入文件
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @Override
    public Map ManAndRobot(Map jsonResponse) {
        String position = (String) jsonResponse.get("result");
        position = codeAndCodeService.ManAndCode(position);
        jsonResponse.put("result", position);
        return jsonResponse;
    }

    //进入到robot界面以后第一次请求的方法就是这个，判断编译是否成功，如果未编译或者对局信息未清空，那就要回到loadRobot界面重新编译
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @Override
    public Map AfterLoadRobot(Map postMap) {
        User user = userService.LodeUserMessage(postMap);
        int IsRobotCodeCompile = (int) redisUtil.get(user.getEmail() + "_IsRobotCodeCompile");
        Map<String, String> map = new HashMap<>();
        if (IsRobotCodeCompile == 1) {  //1代表编译完成并且没有对局信息，此时将_IsRobotCodeCompile设置为2代表已经有对局信息了
            redisUtil.set(user.getEmail() + "_IsRobotCodeCompile", 2);
            codeAndCodeService.noCompileInit(user.getEmail());
            map.put("result", "success");
        } else {    //如果_IsRobotCodeCompile等于0或者2就代表请求失败，需要返回LoadRobot界面重新调用LodeRobotCode方法
            map.put("result", "false");
        }
        return map;
    }

    //该方法用于编译人机代码，每个用户都需要在自己的用户目录下编译一次，因为每个用户的棋盘信息都不一样，当对局结束会清空棋盘信息
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @Override
    public String LodeRobotCode(Map map) {
        User user = userService.LodeUserMessage(map);
        int IsRobotCodeCompile = (int) redisUtil.get(user.getEmail() + "_IsRobotCodeCompile");
        if (IsRobotCodeCompile == 0) {
            //如果人机代码尚未编译，那么就调用ManAndCodeInit编译，1代表编译完成可以对战
            if ("success".equals(codeAndCodeService.ManAndCodeInit(user.getEmail()))) {
                redisUtil.set(user.getEmail() + "_IsRobotCodeCompile", 1);
                return "success";
            } else {
                return "fail";
            }
        } else if (IsRobotCodeCompile == 2) { //2代表已经运行过了，需要调用ClearRobotInputFile清空对局信息
            codeAndCodeService.ClearRobotInputFile();
            redisUtil.set(user.getEmail() + "_IsRobotCodeCompile", 1);
            return "success";
        } else { //1代表编译完成可以对战
            return "success";
        }
    }
}
